﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PrinterBudi.Persistence;

namespace PrinterBudi.Desktop
{
    public partial class FormPBTransaction : Form
    {
        BindingSource bindSource;
        PBTransaction transactionNow;
        PBEmployee employeeActive;

        public FormPBTransaction(PBEmployee eNow)
        {
            bindSource = new BindingSource();
            employeeActive = eNow;
            InitializeComponent();
        }

        public FormPBTransaction()
        {
            bindSource = new BindingSource();
            InitializeComponent();
        }

        private void loadUserPhoneNumberList(int num)
        {
            if (num == 0)
            {
                foreach (DataGridViewRow row in dgvQTransaction.Rows)
                {
                    int userId = ((PBTransaction)row.DataBoundItem).PBUser.Id;
                    row.Cells["UserPhone"].Value = PBUserModel.Read(userId).PhoneNumber;
                }
                dgvQTransaction.Refresh();
            }
            else if (num == 1)
            {
                foreach (DataGridViewRow row in dgvPTransaction.Rows)
                {
                    int userId = ((PBTransaction)row.DataBoundItem).PBUser.Id;
                    row.Cells["UserPhone1"].Value = PBUserModel.Read(userId).PhoneNumber;
                }
                dgvPTransaction.Refresh();
            }
            else if (num == 2)
            {
                foreach (DataGridViewRow row in dgvDTransaction.Rows)
                {
                    int userId = ((PBTransaction)row.DataBoundItem).PBUser.Id;
                    row.Cells["UserPhone2"].Value = PBUserModel.Read(userId).PhoneNumber;
                }
                dgvDTransaction.Refresh();
            }
        }

        private void loadEmployeePhoneNumberList(int num)
        {
            if (num == 1)
            {
                foreach (DataGridViewRow row in dgvPTransaction.Rows)
                {
                    int userId = ((PBTransaction)row.DataBoundItem).PBEmployee.Id;
                    row.Cells["EmployeePhone"].Value = PBEmployeeModel.Read(userId).PhoneNumber;
                }
                dgvPTransaction.Refresh();
            }
            else if (num == 2)
            {
                foreach (DataGridViewRow row in dgvDTransaction.Rows)
                {
                    int userId = ((PBTransaction)row.DataBoundItem).PBEmployee.Id;
                    row.Cells["EmployeePhone1"].Value = PBEmployeeModel.Read(userId).PhoneNumber;
                }
                dgvDTransaction.Refresh();
            }
        }

        private void RefreshDGViewAll(int num) // 0 = queued, 1 = printed, 2 = delivered
        {
            if (num == 0)
            {
                bindSource.DataSource = PBTransactionModel.ReadAllQueued();
                dgvQTransaction.DataSource = bindSource;
                loadUserPhoneNumberList(num);
            }
            else if (num == 1)
            {
                bindSource.DataSource = PBTransactionModel.ReadAllPrinted();
                dgvPTransaction.DataSource = bindSource;
                loadUserPhoneNumberList(num);
                loadEmployeePhoneNumberList(num);
            }
            else if (num == 2)
            {
                bindSource.DataSource = PBTransactionModel.ReadAllDelivered();
                dgvDTransaction.DataSource = bindSource;
                loadUserPhoneNumberList(num);
                loadEmployeePhoneNumberList(num);
            }
            
        }

        #region Enter tab
        private void tabQueued_Enter(object sender, EventArgs e)
        {
            RefreshDGViewAll(0);
        }

        private void tabPrinted_Enter(object sender, EventArgs e)
        {
            RefreshDGViewAll(1);
        }

        private void tabDelivered_Enter(object sender, EventArgs e)
        {
            RefreshDGViewAll(2);
        }
        #endregion

        private void FormPBTransaction_Load(object sender, EventArgs e)
        {
            
        }

        #region Close button click
        private void btnQClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region set transaction status
        private void btnSetPrinted_Click(object sender, EventArgs e)
        {
            PBTransactionModel.SetPrinted(transactionNow, employeeActive);
            RefreshDGViewAll(0);
        }

        private void btnSetDelivered_Click(object sender, EventArgs e)
        {
            PBTransactionModel.SetDelivered(transactionNow);
            RefreshDGViewAll(1);
        }
        #endregion

        #region datagridview selection changed
        private void dgvQTransaction_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvQTransaction.SelectedRows.Count > 0)
                transactionNow = (PBTransaction)dgvQTransaction.SelectedRows[0].DataBoundItem;
        }

        private void dgvPTransaction_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvPTransaction.SelectedRows.Count > 0)
                transactionNow = (PBTransaction)dgvPTransaction.SelectedRows[0].DataBoundItem;
        }

        private void dgvDTransaction_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvDTransaction.SelectedRows.Count > 0)
                transactionNow = (PBTransaction)dgvDTransaction.SelectedRows[0].DataBoundItem;
        }
        #endregion

        #region view all
        private void btnQViewAll_Click(object sender, EventArgs e)
        {
            RefreshDGViewAll(0);
        }

        private void btnPViewAll_Click(object sender, EventArgs e)
        {
            RefreshDGViewAll(1);
        }

        private void btnDViewAll_Click(object sender, EventArgs e)
        {
            RefreshDGViewAll(2);
        }
        #endregion

        #region search by date
        private void btnQSearchDate_Click(object sender, EventArgs e)
        {
            bindSource.DataSource = PBTransactionModel.SearchQByDate(dtpQFrom.Value, dtpQTo.Value);
            dgvQTransaction.DataSource = bindSource;
            loadUserPhoneNumberList(0);
        }

        private void btnPSearchDate_click(object sender, EventArgs e)
        {
            bindSource.DataSource = PBTransactionModel.SearchPByDate(dtpPFrom.Value, dtpPTo.Value);
            dgvPTransaction.DataSource = bindSource;
            loadUserPhoneNumberList(1);
            loadEmployeePhoneNumberList(1);
        }

        private void btnDSearchDate_Click(object sender, EventArgs e)
        {
            bindSource.DataSource = PBTransactionModel.SearchDByDate(dtpDFrom.Value, dtpDTo.Value);
            dgvDTransaction.DataSource = bindSource;
            loadUserPhoneNumberList(2);
            loadEmployeePhoneNumberList(2);
        }
        #endregion

        #region search by phone number
        private void btnQSearchPhone_Click(object sender, EventArgs e)
        {
            bindSource.DataSource = PBTransactionModel.SearchQByPhoneNumber(tbQSearchPhone.Text);
            dgvQTransaction.DataSource = bindSource;
            loadUserPhoneNumberList(0);
        }

        private void btnPSearchPhone_Click(object sender, EventArgs e)
        {
            bindSource.DataSource = PBTransactionModel.SearchPByPhoneNumber(tbPSearchPhone.Text);
            dgvPTransaction.DataSource = bindSource;
            loadUserPhoneNumberList(1);
            loadEmployeePhoneNumberList(1);
        }

        private void btnDSearchPhone_Click(object sender, EventArgs e)
        {
            bindSource.DataSource = PBTransactionModel.SearchDByPhoneNumber(tbDSearchPhone.Text);
            dgvDTransaction.DataSource = bindSource;
            loadUserPhoneNumberList(2);
            loadEmployeePhoneNumberList(2);
        }
        #endregion

        private void tabTransaction_TabIndexChanged(object sender, EventArgs e)
        {
            RefreshDGViewAll(0);
            RefreshDGViewAll(1);
            RefreshDGViewAll(2);
        }


    }
}
